import axios from 'axios'
import Element from 'element-ui'

const request = axios.create({
    baseURL: '/',
    timeout: 50000,
    headers: {
        'Content-Type': 'application/json; charset=utf8'
    }
})

request.defaults.withCredentials = false

// Request Interceptor
request.interceptors.request.use(
    config => {
        config.headers['Authorization'] = localStorage.getItem("token")
        return config
    }, error => {
        return Promise.reject(error)
    }
)

// Response Interceptor
request.interceptors.response.use(
    response => {

        let res = response.data
        if (res.code == 200) {
            return response
        } else {
            Element.Message.error(res.message?res.message:'系统异常')
            return Promise.reject(response.data.message)
        }
        return response
    },
    error => {
        return Promise.reject(error)
    }
)

// request.get('/dev/user').then(
//     response => {
//         console.log(response.data)
//     }
// )

export default request